package com.orange.studio.uicore.customviews;

import com.orange.studio.uicore.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

public class WebViewUICore extends LinearLayout {
	private WebView mWebView = null;
	private ProgressWheelUICore mProgress = null;
	private String url="";
	private boolean isProgressBar=true;
	public WebViewUICore(Context context) {
		super(context);
		initView(context, null);
		initListener();
	}
	
	public WebViewUICore(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
		initListener();
	}

	public WebViewUICore(Context context, AttributeSet attrs, int defStyle) {
		this(context, attrs);
		initView(context, attrs);
		initListener();
	}
	private void initView(Context context,AttributeSet attrs){
		TypedArray a=context.getTheme().obtainStyledAttributes(attrs, R.styleable.WebViewUICore, 0, 0);
		try {
			isProgressBar=a.getBoolean(R.styleable.WebViewUICore_isProgressBar, true);	
		} catch (Exception e) {
		}
		finally{
			a.recycle();
		}
		LayoutInflater.from(context).inflate(R.layout.ui_core_webview, this);
		mWebView=(WebView)findViewById(R.id.webViewContent);
		mProgress=(ProgressWheelUICore)findViewById(R.id.progressWheel);
		mProgress.setVisibility((isProgressBar?View.VISIBLE:View.GONE));
		if(isProgressBar){
			mProgress.spin();
		}
		
	}
	private void initListener()
	{
		mWebView.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				if(isProgressBar){
					mProgress.setVisibility(View.VISIBLE);
				}
				mWebView.setVisibility(View.GONE);
			}
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				mProgress.setVisibility(View.GONE);
				mWebView.setVisibility(View.VISIBLE);
			}
		});	
	
	}
	public void loadUrl(String _url){
		url=_url;
		mWebView.loadUrl(url);
	}
}
